﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace ThreadManager.Restrictions
{
    class Restrictions
    {

        private int _cpuUsage;
        private System.Diagnostics.PerformanceCounter _cpuCounter;
        private string _checkConnectionTo ;

        public int ProcessorUsage
        {
            get
            {
                _cpuUsage = (int)_cpuCounter.NextValue();
                return _cpuUsage;
            }
        }
        public Restrictions()
        {
            _cpuUsage = 0;
            _cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
            _checkConnectionTo = "www.google.com";
        }
        public Restrictions( string checkConnectionTo )
        {
            _cpuUsage = 0;
            _cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
            _checkConnectionTo = checkConnectionTo;
        }

        public bool ConnectionExists()
        {
            try
            {
                System.Net.Sockets.TcpClient clnt = new System.Net.Sockets.TcpClient(_checkConnectionTo, 80);
                clnt.Close();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

        /*
        Call this method every time you need to know
        the current cpu usage.
        */    

    }
}
